//
//  TabbarViewController.m
//  lumihui
//
//  Created by oceans on 2018/9/19.
//  Copyright © 2018年 ios. All rights reserved.
//

#import "TabbarViewController.h"
#import <UIImageView+WebCache.h>
//#import <SDWebImage/UIImageView+WebCache.h>
#import "WatchViewController.h"
#import "TaskViewController.h"
#import "UserCenterViewController.h"
#import <SDWebImageDownloader.h>
#import <MBProgressHUD.h>
#import "TipManager.h"

@interface TabbarViewController () <UITabBarControllerDelegate, UITableViewDelegate>

@end

@implementation TabbarViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    
    self.delegate = self;
    
    self.tabBar.tintColor = [UIColor colorWithRed:199/255.0 green:25/255.0 blue:37/255.0 alpha:1.0];
    if (@available(iOS 10.0, *)) {
        self.tabBar.unselectedItemTintColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
    }
    self.tabBar.barTintColor = [UIColor whiteColor];
    
    WatchViewController *con1 = [[WatchViewController alloc] init];
    NavigationViewController *navi1 = [[NavigationViewController alloc] initWithRootViewController:con1];
    navi1.tabBarItem.image = [UIImage imageNamed:@"t_0"];
    navi1.tabBarItem.selectedImage = [[UIImage imageNamed:@"t_0_o"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navi1.title = @"监管中心";
    navi1.tabBarItem.tag = 0;
    
    TaskViewController *con4 = [[TaskViewController alloc] init];
    NavigationViewController *navi4 = [[NavigationViewController alloc] initWithRootViewController:con4];
    navi4.tabBarItem.image = [UIImage imageNamed:@"t_1"];
    navi4.tabBarItem.selectedImage = [[UIImage imageNamed:@"t_1_o"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navi4.title = @"任务中心";
    navi4.tabBarItem.tag = 1;

    UserCenterViewController *mine = [[UserCenterViewController alloc] init];
    NavigationViewController *navi5 = [[NavigationViewController alloc] initWithRootViewController:mine];
    navi5.tabBarItem.image = [UIImage imageNamed:@"t_2"];
    navi5.tabBarItem.selectedImage = [[UIImage imageNamed:@"t_2_o"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navi5.title = @"个人中心";
    navi5.tabBarItem.tag = 2;
    
    defweakself
    
    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:@"http://oss.youmbe.com/assets/apps/parking/background.png"] options:SDWebImageDownloaderHighPriority progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        
        if (finished && error == nil && (image != nil || data != nil)) {
            
            weakself.viewControllers = @[navi1, navi4, navi5];
            weakself.root = navi1;
            if ([UserModel shared].hasLogin == NO) {

                [Utility showLogin];
            }
        }else {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD showHUDAddedTo:weakself.view animated:NO];
            });
            [self performSelector:@selector(shit) withObject:nil afterDelay:5];
        }
    }];
    
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateHighlighted];
}

- (void)shit {
    
    [MBProgressHUD hideHUDForView:self.view animated:NO];
    [TipManager showTip:@"数据请求失败"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UITabBar appearance] setTranslucent:NO];
    
    UIImageView *image = [[UIImageView alloc] init];
    image.image = [UIImage imageNamed:@"item_2"];
    image.contentMode = UIViewContentModeScaleAspectFill;
    image.frame = CGRectMake(0, 0, 33, 33);
    image.center = CGPointMake(self.tabBar.frame.size.width * 0.5, self.tabBar.frame.size.height * 0.5);
    [self.tabBar addSubview:image];
    
//    UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(panGes:)];
//    pan.edges = UIRectEdgeLeft;
//    [self.view addGestureRecognizer:pan];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

- (void)showTasks {
    
}

#pragma mark - tabbarcontroller delegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    self.root = (NavigationViewController *)viewController;
}

- (void)changeSelectIndex:(NSInteger)index {
    
    self.root = (NavigationViewController *)self.viewControllers[index];
    self.selectedIndex = index;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    if ([touch.view isMemberOfClass:[UIView class]]) {
        return YES;
    }
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
